package com.ruoyi.web.controller.project.excelmerge;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.json.JSONObject;
import com.ruoyi.project.excelmerge.domain.AExcelmerge;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.excelmerge.domain.vo.AExcelmergeVo;
import com.ruoyi.project.excelmerge.domain.bo.AExcelmergeBo;
import com.ruoyi.project.excelmerge.service.IAExcelmergeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * excel合并
 *
 * @author ruoyi
 * @date 2024-03-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/excelmerge")
public class AExcelmergeController extends BaseController {

    private final IAExcelmergeService iAExcelmergeService;

    /**
     * 查询excel合并列表
     */
    @SaCheckPermission("project:excelmerge:list")
    @GetMapping("/list")
    public TableDataInfo<AExcelmergeVo> list(AExcelmergeBo bo, PageQuery pageQuery) {
        return iAExcelmergeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出excel合并列表
     */
    @SaCheckPermission("project:excelmerge:export")
    @Log(title = "excel合并", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AExcelmergeBo bo, HttpServletResponse response) {
        List<AExcelmergeVo> list = iAExcelmergeService.queryList(bo);
        ExcelUtil.exportExcel(list, "excel合并", AExcelmergeVo.class, response);
    }

    /**
     * 获取excel合并详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:excelmerge:query")
    @GetMapping("/{id}")
    public R<AExcelmergeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iAExcelmergeService.queryById(id));
    }

    /**
     * 新增excel合并
     */
    @SaCheckPermission("project:excelmerge:add")
    @Log(title = "excel合并", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<AExcelmerge> add(@Validated(AddGroup.class) @RequestBody AExcelmergeBo bo) {
        return R.ok(iAExcelmergeService.insertByBo(bo));
    }

    /**
     * 修改excel合并
     */
    @SaCheckPermission("project:excelmerge:edit")
    @Log(title = "excel合并", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<AExcelmerge> edit(@Validated(EditGroup.class) @RequestBody AExcelmergeBo bo) {
        return R.ok(iAExcelmergeService.updateByBo(bo));
    }

    /**
     * 删除excel合并
     *
     * @param ids 主键串
     */
    @SaCheckPermission("project:excelmerge:remove")
    @Log(title = "excel合并", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iAExcelmergeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 生成Excel
     * 方式；1.生成返回生成的数据流给前端
     * 方式：2.生成的文件id，文件json数据给前端
     */
    @GetMapping("/genExcel/{id}")
    public R<JSONObject> genExcel(@NotNull(message = "主键不能为空")
                                    @PathVariable String id) {
        return R.ok(iAExcelmergeService.genExcel(id));
    }


}
